<template>
  <div class="register">
    <el-form ref="registerRef" :model="registerForm" :rules="registerRules" class="register-form">
      <h2 class="title">vfadmin后台管理系统</h2>
      <h3 class="title">创建新账户</h3>
      <el-form-item prop="username">
        <el-input 
          v-model="registerForm.username" 
          type="text" 
          size="large" 
          auto-complete="off" 
          placeholder="账号"
        >
          <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="registerForm.password"
          type="password"
          size="large" 
          auto-complete="off"
          placeholder="密码"
          @keyup.enter="handleRegister"
        >
          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword">
        <el-input
          v-model="registerForm.confirmPassword"
          type="password"
          size="large" 
          auto-complete="off"
          placeholder="确认密码"
          @keyup.enter="handleRegister"
        >
          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          size="large" 
          v-model="registerForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter="handleRegister"
        >
          <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
        </el-input>
        <div class="register-code">
          <img :src="codeUrl" @click="getCode" class="register-code-img"/>
        </div>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="large" 
          type="primary"
          style="width:100%;"
          @click.prevent="handleRegister"
        >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2025 insistence.tech All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox } from "element-plus";
import { getCodeImg, register } from "@/api/login";

const router = useRouter();
const { proxy } = getCurrentInstance();

const registerForm = ref({
  username: "",
  password: "",
  confirmPassword: "",
  code: "",
  uuid: ""
});

const equalToPassword = (rule, value, callback) => {
  if (registerForm.value.password !== value) {
    callback(new Error("两次输入的密码不一致"));
  } else {
    callback();
  }
};

const registerRules = {
  username: [
    { required: true, trigger: "blur", message: "请输入您的账号" },
    { min: 2, max: 20, message: "用户账号长度必须介于 2 和 20 之间", trigger: "blur" }
  ],
  password: [
    { required: true, trigger: "blur", message: "请输入您的密码" },
    { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" },
    { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }
  ],
  confirmPassword: [
    { required: true, trigger: "blur", message: "请再次输入您的密码" },
    { required: true, validator: equalToPassword, trigger: "blur" }
  ],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
};

const codeUrl = ref("");
const loading = ref(false);
const captchaEnabled = ref(true);

function handleRegister() {
  proxy.$refs.registerRef.validate(valid => {
    if (valid) {
      loading.value = true;
      register(registerForm.value).then(res => {
        const username = registerForm.value.username;
        ElMessageBox.alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", "系统提示", {
          dangerouslyUseHTMLString: true,
          type: "success",
        }).then(() => {
          router.push("/login");
        }).catch(() => {});
      }).catch(() => {
        loading.value = false;
        if (captchaEnabled) {
          getCode();
        }
      });
    }
  });
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      registerForm.value.uuid = res.uuid;
    }
  });
}

getCode();
</script>

<style lang='scss' scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background: linear-gradient(135deg, #36b543 0%, #9b4cea 100%);
  position: relative;
  overflow: hidden;
}

/* 现代化背景动效 */
.register::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle at 30% 20%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
              radial-gradient(circle at 70% 80%, rgba(255, 255, 255, 0.08) 0%, transparent 50%),
              radial-gradient(circle at 20% 90%, rgba(255, 255, 255, 0.06) 0%, transparent 50%);
  animation: float-bg 25s ease-in-out infinite;
  pointer-events: none;
}

.register::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: 
    linear-gradient(60deg, transparent 48%, rgba(255, 255, 255, 0.02) 49%, rgba(255, 255, 255, 0.02) 51%, transparent 52%),
    linear-gradient(-60deg, transparent 48%, rgba(255, 255, 255, 0.02) 49%, rgba(255, 255, 255, 0.02) 51%, transparent 52%);
  background-size: 40px 40px;
  animation: pattern-move 35s linear infinite;
  pointer-events: none;
}

@keyframes float-bg {
  0%, 100% {
    transform: translate(0, 0) rotate(0deg);
  }
  20% {
    transform: translate(-3%, -7%) rotate(1deg);
  }
  40% {
    transform: translate(7%, -2%) rotate(-0.5deg);
  }
  60% {
    transform: translate(-2%, 6%) rotate(0.8deg);
  }
  80% {
    transform: translate(4%, -4%) rotate(-0.3deg);
  }
}

@keyframes pattern-move {
  0% {
    background-position: 0 0, 0 0;
  }
  100% {
    background-position: 40px 40px, -40px -40px;
  }
}
.title {
  margin: 0px auto 35px auto;
  text-align: center;
  color: #333;
  font-size: 26px;
  font-weight: 600;
  letter-spacing: 2px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.register-form {
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  width: 420px;
  padding: 35px 30px 20px 30px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.1), 
              0 10px 30px rgba(0, 0, 0, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  z-index: 10;
  
  .el-input {
    height: 45px;
    margin-bottom: 5px;
    input {
      height: 45px;
      border-radius: 12px;
      border: 2px solid #f0f0f0;
      transition: all 0.3s ease;
      font-size: 14px;
      
      &:focus {
        border-color: #36b543;
        box-shadow: 0 0 0 3px rgba(54, 181, 67, 0.1);
      }
    }
  }
  .input-icon {
    height: 39px;
    width: 16px;
    margin-left: 5px;
    color: #666;
  }
  
  /* 按钮样式优化 */
  .el-button--primary {
    background: linear-gradient(135deg, #36b543 0%, #9b4cea 100%);
    border: none;
    border-radius: 12px;
    font-weight: 600;
    letter-spacing: 1px;
    transition: all 0.3s ease;
    height: 45px;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 10px 25px rgba(54, 181, 67, 0.3);
    }
  }
  
  /* 链接样式 */
  .link-type {
    color: #36b543;
    text-decoration: none;
    font-size: 14px;
    transition: all 0.3s ease;
    
    &:hover {
      color: #9b4cea;
      text-decoration: underline;
    }
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #888;
  margin-top: 10px;
}

.register-code {
  width: 33%;
  height: 45px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
    border-radius: 8px;
    border: 2px solid #f0f0f0;
    transition: all 0.3s ease;
    
    &:hover {
      border-color: #36b543;
      transform: scale(1.02);
    }
  }
}

.el-register-footer {
  height: 50px;
  line-height: 50px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.9);
  font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
  font-size: 13px;
  letter-spacing: 1px;
  backdrop-filter: blur(5px);
  background: rgba(255, 255, 255, 0.1);
  z-index: 5;
}

.register-code-img {
  height: 41px;
  padding-left: 12px;
  border-radius: 8px;
}
</style>
